﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MQTT试用
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("MQTT结合RabbitMQ3.5.9.0试用，客户端从：https://github.com/eclipse/paho.mqtt.m2mqtt下载");
            //http://blog.csdn.net/leytton/article/details/51896738
            uPLibrary.Networking.M2Mqtt.MqttClient client = new uPLibrary.Networking.M2Mqtt.MqttClient("192.168.2.110");
            Console.WriteLine("输入Key");
            string clientKey = Console.ReadLine();

            //生成字符串
            //client.Connect(Guid.NewGuid().ToString());
            byte result = client.Connect(clientKey, "rabbitmq","rabbitmq");

            //可变头部第一个字节为保留，无甚用处。第二个字节为连接握手返回码：
            //返回值 16进制 含义
            //0   0x00    Connection Accepted
            //1   0x01    Connection Refused: unacceptable protocol version
            //2   0x02    Connection Refused: identifier rejected
            //3   0x03    Connection Refused: server unavailable
            //4   0x04    Connection Refused: bad user name or password
            //5   0x05    Connection Refused: not authorized
            //6 - 255       Reserved for future use

            Dictionary<byte, string> d = new Dictionary<byte, string>() {
                { 0x00    ,"Connection Accepted" },
                {   0x01    ,"Connection Refused: unacceptable protocol version" },
                 {  0x02    ,"Connection Refused: identifier rejected" },
                   {0x03    ,"Connection Refused: server unavailable" },
                  {0x04    ,"Connection Refused: bad user name or password" },
                  {0x05    ,"Connection Refused: not authorized" },
            };

            if (result != 0)
            {
                Console.WriteLine(d[result]);
                return;
            }


            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            //可以从rabbitmq中通过不同的routeKey发送消息到各个客户端中
            订阅(client);
            //表示服务端保存会话信息
            //client.CleanSession = false;


            while (true)
            {


                Console.WriteLine("按任意键再发一条信息,x发送upReport.#RouteKey看看是否能发到服务端的独立队列");
                var x = Console.ReadKey();
                if (x.Key == ConsoleKey.X)
                {

                    client.Publish("upReport." + client.ClientId, System.Text.Encoding.UTF8.GetBytes("" + DateTime.Now + Guid.NewGuid()));

                }
                else
                {
                    client.Publish("r.abc", System.Text.Encoding.UTF8.GetBytes("" + DateTime.Now + Guid.NewGuid()));

                }
            }
        }

        /// <summary>
        /// 订阅得到的消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void client_MqttMsgSubscribed(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgSubscribedEventArgs e)
        {

        }



        static void 订阅(uPLibrary.Networking.M2Mqtt.MqttClient client)
        {
            string s = "r.#";

            client.Subscribe(new string[] { s }, new[] { uPLibrary.Networking.M2Mqtt.Messages.MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
            Console.WriteLine("订阅:{0}", s);

        }




        static void client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
        {
            Console.WriteLine("接收到数据{0}", System.Text.Encoding.UTF8.GetString(e.Message));
            // throw new NotImplementedException();
        }
    }
}
